//
//  JQScorllView.m
//  handworker
//
//  Created by apple on 15/9/12.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "JQScorllView.h"
#import "Team9Header.h"
#import "Team9JQHeader.h"
#import "Team9EveningType.h"

@interface JQScorllView ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *scollView;
@property (nonatomic, strong) UIPageControl *page;

@property (nonatomic, strong) NSArray<NSString *> *slideArray;
@property (nonatomic, weak) UITableView *subView;
@end

@implementation JQScorllView


+(instancetype)jq_scrollViewWithFram:(CGRect)frame
                   OnSubview:(UITableView *)subView
{
    JQScorllView  *view = [[JQScorllView alloc] init];
    view.subView = subView;
    [view creatScorllViewWithFram:frame];
    [[NSNotificationCenter defaultCenter] addObserver:view selector:@selector(changecolor) name:@"changecolor" object:nil];
    return view;
}

- (void)jq_reloadByArray:(NSArray<NSString *> *)slideArray
{
    self.slideArray = slideArray;
    [self p_makeScrolliew];
}
- (void)changecolor
{
    Team9EveningType *evening = [Team9EveningType shareEvening];
    if ([evening eveningFailling]) {
        _scollView.backgroundColor = [UIColor blackColor];
        _page.currentPageIndicatorTintColor = [UIColor grayColor];
    } else{
        _scollView.backgroundColor = [UIColor whiteColor];
        _page.currentPageIndicatorTintColor = [UIColor orangeColor];
    }
}

- (void)dealloc
{
//    [self removeTimer];
//    [_page release];
//    [_scollView release];
//    [_timer release];
//    [_slideArray release];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
//    [super dealloc];

}

- (void)creatScorllViewWithFram:(CGRect)frame
{
//    CGFloat height = SCROLLVIEWHEGHT + GATECOLLETIONHEGHT + TABLEVIEWVIEWHEGHT;
    self.scollView = [[UIScrollView alloc] initWithFrame:frame];
    UIImageView *backScorllView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"backImage"]];
    backScorllView.frame = CGRectMake(0, 0, self.scollView.frame.size.width, self.scollView.frame.size.height);
    [self.scollView addSubview:backScorllView];
//    [self.subView addSubview:_scollView];
    self.subView.tableHeaderView = _scollView;
    _scollView.pagingEnabled = YES;
    _scollView.delegate = self;
    CGFloat heightByY = self.scollView.frame.origin.y + self.scollView.frame.size.height - 30 * WIDTH / 375;
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(0, heightByY, self.scollView.frame.size.width, 20 * WIDTH / 375)];
    [self.subView addSubview:_page];
    if ([Team9EveningType shareEvening].eveningFailling) {
        _page.currentPageIndicatorTintColor = [UIColor grayColor];
    } else _page.currentPageIndicatorTintColor = [UIColor orangeColor];
    _page.pageIndicatorTintColor = [UIColor whiteColor];
    [_page addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
    
    [self changecolor];
}
- (void)pageAction:(UIPageControl *)pageControl{
    [_scollView setContentOffset:CGPointMake((pageControl.currentPage + 1) * self.scollView.frame.size.width, 0) animated:YES];
}

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
//    [_timer setFireDate:[NSDate distantFuture]];
    [self removeTimer];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
//    [_timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:5]];
    [self addTimer];
    NSInteger page = scrollView.contentOffset.x / self.scollView.frame.size.width;
    if (page == 0) {
        _page.currentPage = self.slideArray.count - 1;
    } else if (page == self.slideArray.count + 1) {
        _page.currentPage = 0;
    } else {
        _page.currentPage = page - 1;
    }
    
    if (page == 0) {
        scrollView.contentOffset = CGPointMake(self.slideArray.count * self.scollView.frame.size.width, 0);
    } else if (page == self.slideArray.count + 1) {
        scrollView.contentOffset = CGPointMake(self.scollView.frame.size.width, 0);
    }
}
- (void)changePic{
    
    if (self.page.currentPage != self.slideArray.count - 1) {
        self.page.currentPage++;
    } else {
        self.page.currentPage = 0;
    }
    CGFloat dx = _scollView.contentOffset.x;
    if (dx != self.scollView.frame.size.width * (self.slideArray.count + 1)) {
        [_scollView setContentOffset:CGPointMake(dx + self.scollView.frame.size.width, 0) animated:YES];
    } else {
        [_scollView setContentOffset:CGPointMake(dx + self.scollView.frame.size.width, 0) animated:YES];
        _scollView.contentOffset = CGPointMake(self.scollView.frame.size.width, 0);
    }
}
- (void)p_makeScrolliew
{
    [_scollView.subviews[0] removeFromSuperview];
//    [self addTimer];
    _page.numberOfPages = self.slideArray.count;
    _scollView.contentSize = CGSizeMake(self.scollView.frame.size.width * (self.slideArray.count + 2), self.scollView.frame.size.height);
    _scollView.contentOffset = CGPointMake(self.scollView.frame.size.width, 0);
    
    NSInteger i = 0;
    for (NSString *pic in self.slideArray) {
        i++;
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.scollView.frame.size.width * i, 0, self.scollView.frame.size.width, self.scollView.frame.size.height)];
        imageView.image = [UIImage imageNamed:@"backImage"];
        [imageView sd_setImageWithURL:[NSURL URLWithString:pic] placeholderImage:[UIImage imageNamed:@"backImage"]];
        imageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        imageView.tag = i + 999;
        [imageView addGestureRecognizer:tap];
        [_scollView addSubview:imageView];
        
    }
    UIImageView *firstImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.scollView.frame.size.width, self.scollView.frame.size.height)];
    firstImageView.image = [UIImage imageNamed:@"backImage"];
    NSString *lastPic = [_slideArray lastObject];
    [firstImageView sd_setImageWithURL:[NSURL URLWithString:lastPic]];
    UITapGestureRecognizer *firstTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    firstImageView.tag = 999 + i;
    firstImageView.userInteractionEnabled = YES;
    [firstImageView addGestureRecognizer:firstTap];
    [_scollView addSubview:firstImageView];
    
    UIImageView *lastImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.scollView.frame.size.width * (i + 1), 0, self.scollView.frame.size.width, self.scollView.frame.size.height)];
    lastImageView.image = [UIImage imageNamed:@"backImage"];
    NSString *firstPic = [_slideArray firstObject];
    [lastImageView sd_setImageWithURL:[NSURL URLWithString:firstPic]];
    UITapGestureRecognizer *lastTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    lastImageView.tag = 1000;
    lastImageView.userInteractionEnabled = YES;
    [lastImageView addGestureRecognizer:lastTap];
    [_scollView addSubview:lastImageView];
}
- (void)tapAction:(UIGestureRecognizer *)tap{
    UIImageView *imageView  = (UIImageView *)tap.view;
    CGAffineTransform transform = imageView.transform;
    if (!self.viewController) {
        return;
    }
    [UIView animateWithDuration:0.5 animations:^{
        imageView.transform = CGAffineTransformScale(transform, 0.9, 0.9);
    } completion:^(BOOL finished) {
        imageView.transform = transform;
        if (self.viewController) {
            [self.viewController  selectScrollViewAtIndex:imageView.tag - 1000];
        }
    }];
}
- (void)addTimer{
    NSTimer *timer = [NSTimer timerWithTimeInterval:5 target:self selector:@selector(changePic) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop]addTimer:timer forMode:NSRunLoopCommonModes];
    self.timer = timer;
}
- (void)removeTimer{
    [self.timer invalidate];
    self.timer = nil;
}
@end
